import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_mwm/impl/impl.dart';
import 'package:flutter_mwm/ui/login.dart';
import 'package:flutter_mwm/util/util.dart';
import 'package:flutter_mwm/widget/text.dart';
import 'package:permission_handler/permission_handler.dart';

import './widget/widget.dart';

//void main() => runApp(MwMApp());
///沉浸式状态栏
void main() {
  runApp(MwMApp());
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MwMApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MwM',
      home: SplashHome(),
    );
  }
}

class SplashHome extends StatefulWidget {
  @override
  State createState() => _SplashHomeState();
}

class _SplashHomeState extends State<SplashHome> implements PermissionImpl {
  int _countdownTime = 3;
  Timer _timer;
  bool visible = false;

  @override
  void initState() {
    super.initState();
    _requestPreMission();
  }

  void _requestPreMission() {
    if (Platform.isAndroid) {
      PermissionUtil.request([
        Permission.phone,
        Permission.storage,
        Permission.locationWhenInUse,
        Permission.camera,
      ], this);
    } else if (Platform.isIOS) {
      PermissionUtil.request([
        Permission.storage,
        Permission.photos,
        Permission.locationWhenInUse,
        Permission.camera,
      ], this);
    }
  }

  @override
  Widget build(BuildContext context) {
    return PhotoWallpaper(
      context,
      fullScreen: true,
      alignment: Alignment.topRight,
      child: Offstage(
        child: _countDown(),
        offstage: !visible,
      ),
    );
  }

  void _startCountDown() {
    const oneSec = const Duration(seconds: 1);
    _timer = Timer.periodic(oneSec, (_) {
      setState(() {
        if (_countdownTime == 1) {
          _link2Login();
          return;
        }
        _countdownTime = _countdownTime - 1;
      });
    });
  }

  Widget _countDown() {
    double margin = 25;
    return TextView(
      '跳过丨${_countdownTime}s',
      textColor: Color(0XFF808080),
      padding: EdgeInsets.symmetric(vertical: 8, horizontal: 10),
      margin: EdgeInsets.fromLTRB(margin, margin, margin, margin),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(
          color: Color(0XFF003355),
          width: 3,
        ),
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      onPressed: _link2Login,
    );
  }

  void _link2Login() {
    if (_timer != null) {
      _timer.cancel();
    }
    Navigator.pushAndRemoveUntil(
        context,
        MaterialPageRoute(builder: (_) => LoginHome()),
        (route) => route == null);
  }

  @override
  void onPermission(Permission permission, bool success) {
    if (Platform.isAndroid) {
      switch (permission.value) {
        case 8:
          Log.out("phone $success");
          break;
        case 14:
          Log.out("storage $success");
          break;
        case 3:
          Log.out("location $success");
          break;
        case 1:
          Log.out("camera $success");
          //PlatformKit.showPackage();
          //PlatformKit.showDevice();
          PlatformKit.package().then((value) {
            visible = true;
            _startCountDown();
          });
          break;
      }
    } else if (Platform.isIOS) {
      switch (permission.value) {
        case 14:
          Log.out("storage $success");
          break;
        case 9:
          Log.out("photos $success");
          break;
        case 5:
          Log.out("location $success");
          break;
        case 1:
          Log.out("camera $success");
          //PlatformKit.showPackage();
          //PlatformKit.showDevice();
          PlatformKit.package().then((value) {
            visible = true;
            _startCountDown();
          });
          break;
      }
    }
  }

  @override
  void dispose() {
    super.dispose();
    if (_timer != null) {
      _timer.cancel();
    }
  }
}
